package com.example.administrator.yuejian23.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian23.Model;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.adapter.AskAdapter;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.example.administrator.yuejian23.Bean.AskList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class AskFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    private Context context;
    private View view;
    private RecyclerView recyc;
    private List<AskList> list;
    private Call<List<AskList>> call;
    private String Id;

    public AskFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_ask, container, false);
        init();
        conn();
        return view;
    }

    private void conn() {
        Retrofit retrofit= Model.getConn();
        RetrofitService impls=retrofit.create(RetrofitService.class);
        call=impls.getAskList(Id);
        call.enqueue(new Callback<List<AskList>>() {
            @Override
            public void onResponse(Call<List<AskList>> call, Response<List<AskList>> response) {
                list=response.body();
                AskAdapter a=new AskAdapter(list,context);
                LinearLayoutManager linear=new LinearLayoutManager(context);
                linear.setOrientation(LinearLayoutManager.VERTICAL);
                recyc.setLayoutManager(linear);
                recyc.setItemAnimator(new DefaultItemAnimator());
                recyc.setAdapter(a);
            }

            @Override
            public void onFailure(Call<List<AskList>> call, Throwable t) {
                Toast.makeText(context,"留言列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void init() {
        context=getActivity();
        Intent intent=getActivity().getIntent();
        Id=intent.getStringExtra("tea_id");
        recyc=(RecyclerView)view.findViewById(R.id.re_ask);
    }


}
